package com.lock.demo.redis.conf;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: lock_demo
 * @Package: com.lock.demo.redis.conf
 * @Author: huat
 * @Date: 2021/5/5 10:31
 * @Version: 1.0
 * redisson配置类
 * 关于redis分布式锁，为了避免redis主节点在加入锁的同时挂掉，没有向子节点同步，
 * 故加入redisson算法
 */
@Configuration
public class ConfigurationClass {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    /**
     * 单机装配Redisson
     * @return
     */
    @Bean
    public RLock getRedisson() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);
        return Redisson.create(config).getLock("lockName");
    }
    /**
     * 实际开发过程中应该为cluster或者哨兵模式，这里以cluster为例
     */
    /**
     * 单机装配Redisson
     * @return
     */
//    @Bean
//    public RedissonClient getRedissons() {
//        String[] url={"127.0.0.1:6379", "127.0.0.2:6379"};
//        Config config = new Config();
//        config.useClusterServers().addNodeAddress(url).setPassword(password);
//        return Redisson.create(config);
//    }
}
